package com.polaris.live.dialog

import android.content.Context
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import com.blankj.utilcode.util.ColorUtils
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.R.color
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.DialogPrivacyConfirmBinding
import com.polaris.live.translation.R.string
import com.polaris.live.utils.route.RouterUtils

/**
 * 隐私协议弹窗
 *
 * @author Created by 长空 on 2023/10/28/009 10:28
 */
class PrivacyConfirmDialog(
    context: Context,
    private val callBack: ((mIsContiue: Boolean) -> Unit),
) :
    CenterPopupView(context) {

    private lateinit var mBinding: DialogPrivacyConfirmBinding

    private var mIsContinue: Boolean = false

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_privacy_confirm
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogPrivacyConfirmBinding.bind(popupImplView)

        val message = context.getString(string.please_read_carefully_and_accept_our)
        val builder = SpannableStringBuilder(message)
        val service = context.getString(string.terms_of_service)
        val privacy = context.getString(string.privacy_policy)
        val serviceIndex: Int = message.indexOf(service)
        val privacyIndex: Int = message.indexOf(privacy)
        if (serviceIndex != -1) {
            builder.setSpan(
                serviceClickableSpan,
                serviceIndex,
                serviceIndex + service.length,
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE
            )
        }
        if (privacyIndex != -1) {
            builder.setSpan(
                privacyClickableSpan,
                privacyIndex,
                privacyIndex + privacy.length,
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE
            )
        }
        mBinding.tvPrivacy.highlightColor = ColorUtils.getColor(R.color.color_transparent)
        mBinding.tvPrivacy.movementMethod = LinkMovementMethod.getInstance()
        mBinding.tvPrivacy.text = builder

        mBinding.ivDelete.setOnSingleClickListener {
            dismiss()
        }

        mBinding.tvConfirm.setOnSingleClickListener {
            mIsContinue = true
            dismiss()
        }
    }

    override fun onDismiss() {
        super.onDismiss()
        callBack.invoke(mIsContinue)
    }

    private var serviceClickableSpan: ClickableSpan = object : ClickableSpan() {
        override fun onClick(widget: View) {
            RouterUtils.routerGoto(CommonConst.Agreement.TERMS_OF_SERVICE)
        }

        override fun updateDrawState(paint: TextPaint) {
            paint.isUnderlineText = true
            paint.color = ColorUtils.getColor(color.color_fe1849)
            paint.bgColor = Color.TRANSPARENT
        }
    }

    private var privacyClickableSpan: ClickableSpan = object : ClickableSpan() {
        override fun onClick(widget: View) {
            RouterUtils.routerGoto(CommonConst.Agreement.PRIVACY_POLICY)
        }

        override fun updateDrawState(paint: TextPaint) {
            paint.isUnderlineText = true
            paint.color = ColorUtils.getColor(color.color_fe1849)
            paint.bgColor = Color.TRANSPARENT
        }
    }

}